<!--
  Copyright 2019 Google LLC

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../components/common/ajax-dialog/ajax-dialog.html">
<link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">
<link rel="import" href="../../bower_components/paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../../bower_components/paper-item/paper-item.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">

<dom-module id="create-issue-dialog">
  <link rel="import" href="../technology/technology.css" type="css">
  <template>
    <style>
      /* local DOM styles go here */
      :host {
        display: block;
      }

      :host .explanation {
        width: 400px;
      }

      :host .recommendation {
        font-size: 12px;
        color: #666;
      }

      :host ajax-dialog > * {
        display: block;
        margin: 8px 0px;
      }
    </style>
    <ajax-dialog
        id="dialog"
        url="/testcase-detail/create-issue"
        title="Create an issue"
        submit-button="Create"
        params="[[computeParams(info, selectedSeverity, ccMe)]]"
        response="{{response}}"
        opened="{{opened}}"
        on-response="handleResponse">
      <div class="explanation">
        This will file a new issue for this testcase. Note that manual issue filing is not recommended, all reproducible testcases will get auto-filed once all tasks are finished.
      </div>
      <br/>
      <template is="dom-if" if="[[info.testcase.security_flag]]">
        <template is="dom-if" if="[[info.security_severity]]">
          <div class="recommendation">Recommended severity: [[info.security_severity]]</div>
        </template>
        <paper-dropdown-menu label="Security Severity">
          <paper-listbox class="dropdown-content" selected-item="{{selectedSeverity}}" fallback-selection="[[computeDefaultSeverity(info.security_severity)]]" attr-for-selected="value">
            <template is="dom-repeat" items="[[info.security_severities]]">
              <paper-item value='[[item.value]]'>[[item.name]]</paper-item>
            </template>
          </paper-listbox>
        </paper-dropdown-menu>
      </template>
      <paper-checkbox value="True" checked="{{ccMe}}">CC me on the issue?</paper-checkbox>
    </ajax-dialog>
  </template>
  <script>
    Polymer({
      is: 'create-issue-dialog',
      properties: {
        info: {
          type: Object,
          notify: true
        },
        selectedSeverity: {
          type: String,
          notify: true,
          value: "2"
        },
        ccMe: {
          type: Boolean,
          notify: true,
          value: false
        },
        response: Object,
        opened: {
          type: Boolean,
          notify: true
        }
      },
      computeParams(info, selectedSeverity, ccMe) {
        if (!info || !info.testcase) { return; }

        let params = {
          'testcaseId': info.testcase.id,
          'ccMe': ccMe,
          'csrf_token': info.csrf_token,
        };

        if (info.testcase.security_flag) {
          params.severity = info.security_severity;
          if (selectedSeverity) {
            params.severity = selectedSeverity.value;
          }
        }

        return params;
      },
      computeDefaultSeverity(defaultSeverity) {
        let selected = this.info.security_severities.find((severity) => {
          return severity.name == defaultSeverity || (!defaultSeverity && severity.default);
        });

        return selected ? selected.value : null;
      },
      handleResponse() {
        this.info = this.response;
        this.$.dialog.close();
      }
    });
  </script>
</dom-module>

